"""
show_soma.py

Show somatic epsc
"""

import matplotlib.pyplot as plt
import numpy as np

from neuron import h
from CA3.library import jonas1993

cell = jonas1993()

# prepare synapse at the soma (see Jonas et al., 1993, page 624)
mysyn = h.synapse(0.5, cell.soma)
mysyn.tonset = 0.5 # in ms
mysyn.tau0 = .2    # in ms (tau onset in Jonas et al., 1993)
mysyn.tau1 = 2.5   # in ms (tau decay in Jonas et al., 1993)
mysyn.gmax = 300e-6  # (300 pS, see Jonast et al., page 649)

# voltage clamp mechanism
VC_patch = h.SEClamp(0.5)
VC_patch.rs = 0.1 # series resistance
VC_patch.amp1 = -70 # holding potential (in mV) 

def simulate(tstop):
    """
    simulate the somatic EPSC and
    returns the time (in ms) and current (in pA)
    """
    
    # simulation details
    h.load_file('stdrun.hoc')
    h.v_init = -70
    time, current = h.Vector(), h.Vector()

    time.record(h._ref_t)
    current.record(VC_patch._ref_i)
    
    h.tstop = tstop
    VC_patch.dur1 = tstop
    
    h.run()
    # note that current is in pA
    return (time, np.array(current)*1000.)



time, current = simulate(10)
# peak conductance
ipeak = np.min(current)*1000
print("Peak conductance = %f pS"%(ipeak/-70))
# add gaussian noise
# noise = np.random.normal(0, 1.5, len(current))
# current +=noise

plt.plot(time, current, 'r')
plt.xlabel('Time (ms)')
plt.ylabel('Current (pA)')
plt.show()
